﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CMSWindowsFormsApp2
{
    public partial class CustomerForm : Form
    {
        private SqlConnection con;
        SqlDataAdapter adapter;
        DataSet customerDataSet;
        BindingSource bindingSource;

        public CustomerForm()
        {
            InitializeComponent();
        }

        private void CustomerForm_Load(object sender, EventArgs e)
        {

            con= new SqlConnection("Data Source=DESKTOP-SR1MKQK;Initial Catalog=CMSDATABASE;Integrated Security=True");
            adapter=  new SqlDataAdapter("Select * from tblCustomer", con);
            customerDataSet = new DataSet();
            SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(adapter);
            adapter.Fill(customerDataSet,"tblCustomer");//fix 1

            bindingSource = new BindingSource();
            bindingSource.DataSource = customerDataSet;
            bindingSource.DataMember = "tblCustomer";

            txtCarNo.DataBindings.Add("Text", bindingSource, "CarNo");
            txtName.DataBindings.Add("Text", bindingSource, "Name");
            txtAddress.DataBindings.Add("Text", bindingSource, "address");
            txtModel.DataBindings.Add("Text", bindingSource, "make");// fix 2 make not model

            UpdatePosition();


        }

        private void UpdatePosition()
        {
            lblPosition.Text = $"{bindingSource.Position + 1} of {bindingSource.Count}";
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (bindingSource.Position < bindingSource.Count - 1)
            {
                bindingSource.Position += 1;//next
            }
            UpdatePosition();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (bindingSource.Position > 0) {
                bindingSource.Position -= 1;//pre
            }
            UpdatePosition();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            DataRow newRow = customerDataSet.Tables["tblCustomer"].NewRow();
            newRow["CarNo"] = "";
            newRow["name"] = "";
            newRow["address"] = "";
            newRow["make"] = "";

            customerDataSet.Tables["tblCustomer"].Rows.Add(newRow);

            bindingSource.Position = bindingSource.Count - 1;
            txtCarNo.ReadOnly = false;
            txtName.ReadOnly = false;
            txtAddress.ReadOnly = false;
            txtModel.ReadOnly = false;

            UpdatePosition();

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try {
                bindingSource.EndEdit();
                adapter.Update(customerDataSet, "tblCustomer");
                MessageBox.Show("Record Saved Successfully!");
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            txtCarNo.ReadOnly = false;
            txtName.ReadOnly = false;
            txtAddress.ReadOnly = false;
            txtModel.ReadOnly = false;

        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            bindingSource.CancelEdit();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
